package com.ebupt.migu.music.commons.selfdom.demo.impl;

import com.ebupt.migu.globalpopup.entity.ConditionRelation;
import com.ebupt.migu.globalpopup.enums.ConditionEnum;
import com.ebupt.migu.globalpopup.enums.OperatorEnum;
import com.ebupt.migu.music.commons.selfdom.demo.Province;
import com.ebupt.migu.music.commons.selfdom.demo.impl.identifier.AbstractIdentifierImpl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 省份运营子条件实现类
 *
 * @author wangzhu
 * @since 2021-12-31
 */
public class ProvinceImpl extends AbstractIdentifierImpl implements Province {
    /**
     * 运营商：不清楚运营商类型，中国移动，中国联通，中国电信
     */
    private boolean operator(String data, ConditionRelation conditionRelation,Integer conditionKey) {

        String nameByCode = OperatorEnum.getNameByCode(conditionRelation.getConditionData());

        return super.getIdentifier(data, nameByCode, conditionRelation.getRelation(),conditionKey);
    }

    /**
     * 省份
     */
    private boolean province (String data, ConditionRelation conditionRelation,Integer conditionKey) {
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }
    /**
     * 城市
     */
    private boolean city (String data, ConditionRelation conditionRelation,Integer conditionKey) {
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }
    /**
     * ip地理位置
     */
    private boolean ip(String data, ConditionRelation conditionRelation,Integer conditionKey) {
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }
    /**
     * 设备ip
     */
    private boolean equipmentIp(String data, ConditionRelation conditionRelation,Integer conditionKey) {
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }

    /**
     * 更具不同的子条件判断返回判断结果（子条件第一版只支持or）
     */
    @Override
    public boolean getProvince(Map data, List<ConditionRelation> conditionRelations) {
        LinkedHashMap<Boolean, Integer> booleanIntegerLinkedHashMap = new LinkedHashMap<>();
        // 排序
        super.sort(conditionRelations);
        for (ConditionRelation conditionRelation : conditionRelations) {
            boolean province = true;
            Object o = data.get(ConditionEnum.getKeyValue(conditionRelation.getPropertyCode(), conditionRelation.getConditionKey()));
            if (o==null||o.equals("")) {
                province = false;
                booleanIntegerLinkedHashMap.put(province,conditionRelation.getCConnector());
                break;
            }
            String value = o.toString();
            switch (conditionRelation.getConditionKey()) {
                case 1:
                    province = this.operator(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                case 2:
                    province = this.province(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                case 3:
                    province = this.city(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                case 4:
                    province = this.ip(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                case 5:
                    province = this.equipmentIp(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                default:
                    break;
            }
            booleanIntegerLinkedHashMap.put(province, conditionRelation.getCConnector());
        }
        return super.getRelation(booleanIntegerLinkedHashMap);
    }

}
